package start.demo01.datatype.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.junit.Test;
import start.demo01.datatype.json.entity.DataFactory;
import start.demo01.datatype.json.entity.editor.jsonEditor.TimeFormatEditor;
import start.demo01.datatype.json.interfaces.Editor;
import start.demo01.datatype.json.interfaces.Factory;
import start.demo01.datatype.json.interfaces.JSONEditor;
import start.demo01.datatype.json.pool.CacheMap;

import java.io.*;
import java.nio.charset.Charset;

public class Demo {

    public static void main(String[] args) {
        new Demo().t0();
    }

    @Test
    public void t0() {
//        StringBuilder cmdBuilder = new StringBuilder();
//        cmdBuilder
//                .append("S $ strToJSON>m").append('\n')
//                .append("J $m store[0][*].remove(x,y)").append('\n')
//                .append("J $m store[0][0].timeFormat({time,time,yyyy-MM-dd HH:mm:ss})")
//                .append(".timeFormat({yyyy-MM-dd HH:mm:ss,ftime,time})").append('\n')
//                .append("J $m store[0][1].insert({test,time}).insert({test,yyyy-mm})").append('\n')
//                .append("J $m store[0][0].get(mv,time)").append('\n')
//                .append("J $m store[0][0].reserve(time)").append('\n')//保留指定属性
//                .append("return $mv").append('\n')
//                .append("J $map userAgent.underline(true)").append('\n')
//                .append("S $time replace([a-z],-)").append('\n')//正则替换

//                .append("S strToJSON(m)").append('\n')
//                .append("J $m get(message)").append('\n')
//                .append("S $message truncation(var,{,0)").append('\n')//截取并创建新变量var
//                .append("S $var strToJSON()").append('\n')
//                .append("S $message truncation(str,,0,{,-1)").append('\n')//截取并创建新变量str
//                .append("S $str truncation(time,>,1,>,15)").append('\n')//截取并创建新变量time
//                .append("J $var get(map,actor)").append('\n')
//                .append("J $var client.copy(map,,*)").append('\n')//复制当前对象内容到指定对象指定位置
//                .append("J $map remove(userAgent)").append('\n')//复制当前对象内容到指定对象指定位置
//                .append("J $map reserve(ip,type,deviceName,groupPath,device)").append('\n')//保留
//                .append("J $map insert({operaTime,yyyy-MM-dd},{time,$time})").append('\n')
//                .append("return $map")
//        ;
        String config = "D:\\FilePath\\IDEAproject\\basics\\src\\main\\java\\start\\demo01\\datatype\\json\\config.txt";
        String path = "D:\\FilePath\\IDEAproject\\basics\\src\\main\\java\\start\\demo01\\datatype\\json\\json\\zreo.json";
        String content = getContent(path);
        config=getContent(config);
        Factory<String, Object> factory = new DataFactory(config);//线程不安全

        CacheMap cache = new CacheMap();
        Object resolve = factory.resolve(cache,content);

        if (resolve instanceof JSONObject)
            JSONEditor.print("解析结果->\n", resolve);
        else
            System.out.println("解析结果->\n" + resolve);
        System.out.println("解析完成");
    }
    private static String getContent(String path) {
//        String path = "D:\\FilePath\\IDEAproject\\basics\\src\\main\\java\\start\\demo01\\datatype\\json\\json\\test.json";
        File file = new File(path);
        String content = null;
        try (InputStream in = new FileInputStream(file)) {
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            content = new String(bytes, "UTF-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    @Test
    public void t1() {
        Editor.getName("");
    }

    @Test
    public void time() {
//        Object val = TimeFormatEditor.trans("yyyyMMddHHmmss", "20221206144200", "yyyy-MM-dd HH:mm:ss");
//        Object val = TimeFormatEditor.trans("yyyyMMddHHmmss", "20221206144200", "time");

        String val = TimeFormatEditor.time2Format(1670308920000l, "yyyy-MM-dd HH:mm:ss");
        System.out.println(val);
    }
}
